package com.swift.violent.mainstay.common.lang;

import com.swift.violent.mainstay.common.constants.ResultCode;
import com.swift.violent.mainstay.common.constants.ResultDesc;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用返回结果
 *
 * @param <T> the type parameter
 *
 * @author xuhaifeng
 * @since 2019.12.11
 */
@Data
@NoArgsConstructor
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal", "unused"})
public class CommonResultOutput<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 编码
     */
    private Integer code;
    /**
     * 描述信息
     */
    private String desc;
    /**
     * 响应数据
     */
    private T data;

    /**
     * Instantiates a new Common result output.
     *
     * @param code the resp code
     * @param desc the resp desc
     * @param data the data
     */
    private CommonResultOutput(final Integer code, final String desc, final T data) {
        this.code = code;
        this.desc = desc;
        this.data = data;
    }

    /**
     * Instantiates a new Common result output.
     *
     * @param code the resp code
     * @param desc the resp desc
     */
    private CommonResultOutput(final Integer code, final String desc) {
        this.code = code;
        this.desc = desc;
    }

    /**
     * Common output json object.
     *
     * @param respCode the resp code
     * @param respDesc the resp desc
     * @param data     the data
     *
     * @return the json object
     */
    public static <T> CommonResultOutput<T> commonOutput(final Integer respCode, final String respDesc, final T data) {
        return new CommonResultOutput<>(respCode, respDesc, data);
    }

    /**
     * Common output json object.
     *
     * @return the json object
     */
    public static <T> CommonResultOutput<T> commonSuccessOutput() {
        return new CommonResultOutput<>(ResultCode.OK.code, ResultDesc.OK.desc);
    }

    /**
     * Common output json object.
     *
     * @param data the data
     *
     * @return the json object
     */
    public static <T> CommonResultOutput<T> commonSuccessOutput(final T data) {
        return new CommonResultOutput<>(ResultCode.OK.code, ResultDesc.OK.desc, data);
    }

    /**
     * Common error output json object.
     *
     * @param respDesc the resp desc
     *
     * @return the json object
     */
    public static <T> CommonResultOutput<T> commonErrorOutput(final Integer respCode, final String respDesc) {
        return new CommonResultOutput<>(respCode, respDesc);
    }

}
